﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RoutingDaemon.Utilities;
using RoutingDaemon.Entities;

namespace RoutingDaemon.ServerInterface.CommandHandlers
{
    class USERTABLECommandHandler : CommandHandlerBase
    {
        public override string HandleCommand(global::RoutingDaemon.ServerInterface.Commands.DaemonCommandBase command)
        {
            if (command is Commands.USERTABLECommand)
            {
                Commands.USERTABLECommand userTableCommand = (Commands.USERTABLECommand)command;

                string userTableEntries = string.Empty;
                string response = string.Empty;
                int size = 0;
                foreach (RoutingTableEntry tableEntry in Backend.DaemonBackEnd.Instance.RoutingTable)
                {
                    foreach (User user in tableEntry.Node.Users)
                    {
                        userTableEntries += Responses.GetResponse(ResponseCodes.UserEntry, new string[] { user.Nickname, tableEntry.Node.NodeID.ToString(), tableEntry.Distance.ToString() });
                        userTableEntries += Environment.NewLine;
                        size++;
                    }
                }
                response = Responses.GetResponse(ResponseCodes.UserTable_OK, new string[] { size.ToString() });
                response = response + Environment.NewLine + userTableEntries;
                return response;
            }
            else
            {
                throw new ArgumentException();
            }

        }
    }
}
